<?php
// +----------------------------------------------------------------------
// | LwwanPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.lwwan.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 星辰工作室 QQ群331378225
// +----------------------------------------------------------------------

namespace app\order\model;

use app\common\model\Order;
use think\Model as ThinkModel;

/**
 * 订单行为记录表
 * @package app\order\model
 */
class OrderAction extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__ORDER_ACTION__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 写入动作
     * @param string $order_sn
     * @param string $action
     * @param int $order_status
     * @param int $user_id
     * @param string $remark
     * @return boolean
     */
    public static function addLog($order_sn, $action, $order_status, $user_id = 0, $remark = ''){        
        $statusName = Order::$order_status[$order_status];
        $data = [
            "order_sn" => $order_sn,
            "action" => $action,
            "user_id" => $user_id,
            "uid" => defined('UID') ? UID : 0,
            "order_status" => $order_status,
            "order_status_text" => $statusName,
            "create_time" => time(),
            "remark" => $remark
        ];
        self::create($data);
        return true;
    }
    public static function actionLog($order_sn, $action, $order_status, $statusName= "", $remark = '', $user_id = 0)
    {
        if(!$statusName){
            $statusName = Order::$order_status[$order_status];
        }
        $data = [
            "order_sn" => $order_sn,
            "action" => $action,
            "user_id" => $user_id,
            "uid" => defined('UID') ? UID : 0,
            "order_status" => $order_status,
            "order_status_text" => $statusName,
            "create_time" => time(),
            "remark" => $remark
        ];
        self::create($data);
        return true;
    }

    public function getActionLogs($where = '')
    {
        return self::alias('oa')
            ->join("admin a", "oa.uid=a.id","left")
            ->field("order_sn,action,order_status,order_status_text,oa.create_time,username,remark")
            ->order("oa.create_time desc")
            ->where($where)
            ->paginate();
    }

}